package it.tcm.TeacherClassMate.action;


import java.io.*;
import javax.servlet.http.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
*
* @author Matej Knopp
* @version
*/

public class URLFilter implements Filter {

// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;

public URLFilter () {
}

/*
* This response wrapper is here to determine if servlet wanted
* to send redirect.
*/
static class ResponseWrapper extends HttpServletResponseWrapper {
public String redirect = null;

ResponseWrapper (HttpServletResponse response) {
super (response);
}

public void sendRedirect (String location) throws IOException {
redirect = location;
}

public void sendRedirectReal (String location) throws IOException {
super.sendRedirect (location);
}
}

String attrName = "URL";

/**
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter (ServletRequest req, ServletResponse resp,FilterChain chain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
ResponseWrapper response = new ResponseWrapper ((HttpServletResponse)
resp);
HttpSession session = request.getSession ();

String iface = request.getParameter ("interface");
if (iface == null)
iface = "";

if (request.getQueryString () == null &&
session.getAttribute (attrName) != null) {
// no query string and url is in session -> forward the request
request.getRequestDispatcher ((String)session.getAttribute
(attrName))
.forward (request, response);
} else if (iface.equalsIgnoreCase ("IRedirectListener") ||
request.getParameter ("bookmarkablePage") != null) {
// we have a bookmarkable page or a redirect listener
// store the URL in session and redirect to a page with no
// query parameters
String url = request.getServletPath ();
url = url + "?" + request.getQueryString () + "&doNotTouch=1";
session.setAttribute (attrName, url);
response.sendRedirectReal (request.getServletPath ().substring
(1));
}
else {
// otherwise do the filter chain
chain.doFilter (request, response);
// did we get redirect?
if (response.redirect != null) {
// yes we did. get the query string
int paramStart = response.redirect.indexOf ("?");
String parameters;
if (paramStart != -1) // do we have one?
parameters = response.redirect.substring (paramStart) +
"&doNotTouch=1";
else
parameters = "?doNotTouch=1";
// put the url requested by redirect to session (it's convertedto local path,
// because of the forwarding
session.setAttribute (attrName, request.getServletPath () +
parameters);
// redirect to a page without query parameters
response.sendRedirectReal (request.getServletPath ().substring
(1));
} else {
// no redirect

// If the render policy is set to render to buffer,
// this will redirect to a page with no query parameters,
// otherwise this will do nothing, since the content of a page
// is already written
String url = request.getServletPath ();
url = url + "?" + request.getQueryString () + "&doNotTouch=1";
// put the current URL in session
session.setAttribute (attrName, url);
// redirect to a page without query parameters
response.sendRedirectReal (request.getServletPath ().substring
(1));
}
}

}

/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig () {
return (this.filterConfig);
}


/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig (FilterConfig filterConfig) {

this.filterConfig = filterConfig;
}

/**
* Destroy method for this filter
*
*/
public void destroy () {
}


/**
* Init method for this filter
*
*/
public void init (FilterConfig filterConfig) {

this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log ("URLFilter:Initializing filter");
}
if (filterConfig.getInitParameter ("attrName") != null) {
this.attrName = filterConfig.getInitParameter ("attrName");
}
}
}

public void log (String msg) {
filterConfig.getServletContext ().log (msg);
}

private static final boolean debug = true;
}